package com.rp.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Map;

/**
 * Excel 导出数据引擎
 */
public class ExcelExportEngine {

    public static Workbook writeToExcel(Object object, Workbook workbook, String imagePath) throws  Exception{

        //1. 将 object转成map
        Map<String, Object> map = EntityUtils.entityToMap(object);

        //获取第一个sheet页
        Sheet sheet = workbook.getSheetAt(0);

        //todo
        //循环100 行 每一行循环100 个单元格
        Row row = null;
        Cell cell= null;

        for (int i = 0; i < 100; i++) {
            row = sheet.getRow(i);

            if(row == null){
                break;
            }else{
                for (int j = 0; j < 100; j++) {
                    cell = row.getCell(j);
                    if(cell != null) {
                        writeToCell(cell,map);
                    }
                }
            }
        }

        if(StringUtils.isNotBlank(imagePath)){
            //先创建一个字节输出流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            //读取图片 放入一个带有缓存区的图片类中
            BufferedImage bufferedImage = ImageIO.read(new File(imagePath));

            //获取图片的后缀名
            String extName = imagePath.substring(imagePath.lastIndexOf(".") + 1).toUpperCase();

            //将图片写入到字节输出流中
            ImageIO.write(bufferedImage,extName,byteArrayOutputStream);


            //Patriarch 控制图片的写入  clientAnchor 指定图片的位置
            Drawing patriarch = sheet.createDrawingPatriarch();

            //指定图片的位置 图片的位置 开始列3 开始行2  结束列4 结束行5
            //前四个单位是: 一个英式公制的单位  1cm = 360000
            Sheet sheetAt = workbook.getSheetAt(1);
            int col1 = ((Double) sheetAt.getRow(0).getCell(0).getNumericCellValue()).intValue();
            int row1 = ((Double) sheetAt.getRow(0).getCell(1).getNumericCellValue()).intValue();
            int col2 = ((Double) sheetAt.getRow(0).getCell(2).getNumericCellValue()).intValue();
            int row2 = ((Double) sheetAt.getRow(0).getCell(3).getNumericCellValue()).intValue();

            //最后连个参数要注意 : 图片的右下角 到指定的单元格的左上角
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, col1, row1, col2, row2);

            //将图片写入到sheet指定的位置

            int format = 0;
            switch (extName){
                case "JPG":{
                    format = XSSFWorkbook.PICTURE_TYPE_JPEG;
                }
                case "JPEG":{
                    format = XSSFWorkbook.PICTURE_TYPE_JPEG;
                }
                case "PNG":{
                    format = XSSFWorkbook.PICTURE_TYPE_PNG;
                }
            }

            patriarch.createPicture(anchor,workbook.addPicture(byteArrayOutputStream.toByteArray(),format));
            workbook.removeSheetAt(1);
        }

        return workbook;
    }

    //比较单元格中的值,是否和map中的key一致,如果一致则将 map中对应的value放入单元格中
    private static void writeToCell(Cell cell, Map<String, Object> map) {
        CellType cellType = cell.getCellType();

        switch (cellType){
            case FORMULA:{
                break;
            }
            default:{
                String cellValue = cell.getStringCellValue();
                if(StringUtils.isNotBlank(cellValue)){
                    for (String key : map.keySet()){
                        if(cellValue.equals(key)){
                            cell.setCellValue(map.get(key).toString());
                        }
                    }
                }
            }
        }
    }
}
